<script setup lang="ts">
import { getMedicalOrderLogistics } from '@/services/order'
import type { Logistics } from '@/types/order'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import AMapLoader from '@amap/amap-jsapi-loader'
import startImg from '@/assets/start.png'
import endImg from '@/assets/end.png'
import carImg from '@/assets/car.png'
import type { Location } from '@/types/order'

const route = useRoute()
// 获取物流信息
const logistics = ref<Logistics>()
onMounted(async () => {
  const res = await getMedicalOrderLogistics(route.params.id as string)
  logistics.value = res.data

  // 加载地图
  window._AMapSecurityConfig = {
    securityJsCode: '415e917da833efcf2d5b69f4d821784b'
  }

  AMapLoader.load({
    key: '4eed3d61125c8b9c168fc22414aaef7e',
    version: '2.0'
  }).then((AMap) => {
    // 使用 Amap 初始化地图
    const map = new AMap.Map('map', {
      mapStyle: 'amap://styles/whitesmoke',
      zoom: 12
    })

    // 画路线图
    AMap.plugin('AMap.Driving', function () {
      const driving = new AMap.Driving({
        map, // map这个地图实例画路线
        showTraffic: false, // 关闭路况信息
        hideMarkers: true // 关闭显示途经点
      })

      if (
        logistics.value?.logisticsInfo &&
        logistics.value.logisticsInfo.length >= 2
      ) {
        const list = [...logistics.value.logisticsInfo]
        // 创建标记函数
        const getMarker = (
          point: Location,
          image: string,
          width = 25,
          height = 30
        ) => {
          // 创建 AMap.Icon 实例：
          const icon = new AMap.Icon({
            size: new AMap.Size(width, height), // 图标尺寸
            image: image, // Icon的图像
            imageSize: new AMap.Size(width, height) // 根据所设置的大小拉伸或压缩图片
          })
          // 将 Icon 实例添加到 marker 上:
          const marker = new AMap.Marker({
            position: [point?.longitude, point?.latitude], // 当前运输位置
            offset: new AMap.Pixel(-width / 2, -height), // 偏移量
            icon: icon // 添加 Icon 实例
          })
          return marker
        }
        // 起点
        const startLngLat = list.shift()
        const startMarker = getMarker(startLngLat!, startImg)
        // 图标添加到地图上
        map.add(startMarker)
        // 终点
        const endLngLat = list.pop()
        const endMarker = getMarker(endLngLat!, endImg)
        // 图标添加到地图上
        map.add(endMarker)
        driving.search(
          [startLngLat?.longitude, startLngLat?.latitude],
          [endLngLat?.longitude, endLngLat?.latitude],
          {
            waypoints: list.map((item) => [item.longitude, item.latitude])
          },
          () => {
            // 规划完毕
            // 创建运输图标
            const curr = logistics.value?.currentLocationInfo
            const currMarker = getMarker(curr!, carImg, 33, 20)
            // 图标添加到地图上
            map.add(currMarker)
            // 三秒后缩放
            setTimeout(() => {
              map.setFitView([currMarker]) //以什么位置为中心缩放
              map.setZoom(10) // 缩放倍数
            }, 3000)
          }
        )
      }
    })
  })
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ logistics?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">
          {{ logistics?.statusValue }} 预计{{ logistics?.estimatedTime }}送达
        </p>
        <p class="predict">
          <span>{{ logistics?.name }}</span>
          <span>{{ logistics?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in logistics?.list" :key="item.id">
          <p class="status">{{ item.statusValue }}</p>
          <p class="content">
            {{ item.content }}
          </p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}

#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;

  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;

    > span {
      flex: 1;
      text-align: center;
    }

    .van-icon {
      font-size: 18px;
      color: #666;

      &:last-child {
        color: var(--cp-primary);
      }
    }
  }

  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;

    .status {
      font-size: 15px;
      line-height: 26px;
    }

    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;

      > span {
        padding-right: 10px;
      }
    }
  }
}

.logistics {
  padding: 0 10px 20px;

  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }

  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }

    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }

    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }

    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
